﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fashion.Models;
using System.Globalization;

namespace Fashion.ViewModels
{
    public class StoreDetailViewModel
    {
        public int productID;
        public string Description {get ; set; } //Product description
        public string Brand {get; set;}
        public string Category { get; set; }
        public List<string> Colors {get ; set; } //Colors of product
        public List<string> Sizes { get; set; }
        public Product Product {get ; set; }

        string langID = "EN"; //suppose that default language is EN
        private FashionEntities1 FashionDB = new FashionEntities1();

        public StoreDetailViewModel(int ID)
        {
            this.productID = ID;
            GetDescription();
            GetProduct();
        }

        public void GetDescription()
        {

            var description = from product in FashionDB.Product_translation
                                 where product.ProductID == productID && product.LanguageID == langID
                                 select product.Description;

            this.Description = description.ToString();
        }

        public void GetProduct()
        {
            // get brand name
            var brandModel = (from product in FashionDB.Products
                             where product.ProductID == this.productID
                             select product.Brand.BrandName).First();

            this.Brand = brandModel.ToString();

            var categoryModel = (from product in FashionDB.Products
                                where product.ProductID == productID
                                select product.Category.Name).First();

            this.Category = categoryModel.ToString();

            var productModel = from product in FashionDB.Products
                               where product.ProductID == productID
                               select product;

            this.Product = productModel.First();

            // Load image list for product
            var colorList = from detail in FashionDB.ProductDetails
                            join color in FashionDB.Colors
                            on detail.ColorID equals color.ColorID
                            where detail.ProductID == productID
                            select color.Name;
            // set color list
            this.Colors = new List<string>(colorList);
            

            // format image url
            this.Product.imageList = new List<string>();
            

            //image_prefix have the form Women/W1, Men/M30....
            int iIndex = this.Product.ImageList.IndexOf('_');
            string image_prefix = this.Product.ImageList.Remove(iIndex);

            this.Sizes = new List<string>(Colors.Count());
            foreach (var color in colorList)
            {
                string image = image_prefix + "_"
                    + CultureInfo.CurrentCulture.TextInfo.ToTitleCase(color.ToString()) + ".jpg";
                
                /* Old code
                string image = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(this.Product.Sex) + "/" + CultureInfo.CurrentCulture.TextInfo.ToTitleCase(this.Product.ProductName)
                    + "_" + CultureInfo.CurrentCulture.TextInfo.ToTitleCase(color.ToString()) + ".jpg";
                 * */
                this.Product.imageList.Add(image);
                // For each color get its size lists available
                Sizes.Add(GetSizeByColor(color.ToString()));
            }
        }

        public string GetSizeByColor(string color)
        {
            var sizeList = (from colors in FashionDB.Colors
                           join detail in FashionDB.ProductDetails
                           on colors.ColorID equals detail.ColorID
                           where colors.Name == color && detail.ProductID == productID
                           select detail.SizeList).First();
            string sizes = sizeList.ToString();
            return (sizes);
        }
    }
}
